package com.llkj.qingnianguoxue.music;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MusicMineActivity extends BaseActivity {

    @BindView(R.id.fl_music_publish)
    protected FrameLayout flMusicPublish;
    @BindView(R.id.fl_music_local)
    protected FrameLayout flMusicLocal;
    private MusicMineFragment mPublishFragment,mLocalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_mine);
        ButterKnife.bind(this);
        setTitle("我唱的", true, TITLE_TYPE_IMG, R.drawable.icon_back, false,-1,-1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);

        setSelection(0);
    }

    @OnClick({R.id.fl_music_publish, R.id.fl_music_local})
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.fl_music_publish:
                setSelection(0);
                break;
            case R.id.fl_music_local:
                setSelection(1);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flMusicPublish);
                if (mPublishFragment == null) {
                    mPublishFragment = MusicMineFragment.newInstance("0");
                    fragmentTransaction.add(R.id.content_layout, mPublishFragment, "tab0");
                } else {
                    fragmentTransaction.show(mPublishFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flMusicLocal);
                if (mLocalFragment == null) {
                    mLocalFragment = MusicMineFragment.newInstance("1");
                    fragmentTransaction.add(R.id.content_layout, mLocalFragment, "tab1");
                } else {
                    fragmentTransaction.show(mLocalFragment);
                }
                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flMusicPublish.setSelected(false);
        flMusicLocal.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (mPublishFragment != null) {
            transaction.hide(mPublishFragment);
        }
        if (mLocalFragment != null) {
            transaction.hide(mLocalFragment);
        }
    }
}
